﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using CardGamesLibrary.Poker;

namespace CardGamesLibrary.UI.Poker
{
    public partial class PokerTableForm : Form
    {
        private readonly PokerGameBase _gameBase;
        private readonly PokerTable _table;
        private Thread _gameThread;

        public PokerTableForm(PokerGameBase gameBase) : this()
        {
            if (gameBase == null) throw new ArgumentNullException("gameBase");

            _gameBase = gameBase;
            _table = gameBase.Table as PokerTable;
        }

        public PokerTableForm()
        {
            InitializeComponent();
        }

        private void PokerTableForm_Load(object sender, EventArgs e)
        {
            pokerTableControl1.AssignTable(_table);
            _gameThread = new Thread(new ParameterizedThreadStart(GameThread));
            _gameThread.Start(null);
        }

        private void GameThread(object obj)
        {
            while (!_gameBase.Finished)
            {
                _gameBase.CreateAndPlayNewTurn();
                Thread.Sleep(200);
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            _gameThread.Abort();
            base.OnClosing(e);
        }
    }
}
